<template>
  <BaseModal
    :visible="true"
    title="模型切换"
    :width="560"
    cancelText="取消"
    okText="确认"
    v-on="$listeners"
    @ok="handleConfirm"
  >
    <a-form-model
      ref="form"
      :model="formInfo"
      :rules="rules"
      :labelCol="{ span: 6 }"
      :wrapperCol="{ span: 18 }"
      hideRequiredMark
    >
      <a-form-model-item label="模型" prop="modelId">
        <a-select v-model="formInfo.modelId" style="width: 200px">
          <a-icon v-if="loading.isFetching" style="color: #096dd9" slot="suffixIcon" type="loading" />
          <a-spin v-if="loading.isFetching" slot="notFoundContent" size="small" />
          <a-select-option v-for="item in modelList" :key="item.modelId">{{ item.modelName }}</a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
  </BaseModal>
</template>

<script>
import { getBindList } from '@/api/bimIntegrate/business.js'
import BaseModal from '@/components/BaseModal'
export default {
  components: {
    BaseModal,
  },
  props: {
    defaultValue: {
      type: String,
      default: '',
    },
    projectId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      loading: {
        isFetching: false,
      },
      formInfo: {
        modelId: '',
      },
      modelList: [],
      rules: {
        modelId: [{ required: true, message: '请选择模型', trigger: 'change' }],
      },
    }
  },
  created() {
    this.initPage()
    this.getBindList()
  },
  methods: {
    initPage() {
      this.formInfo.modelId = this.defaultValue
    },
    async getBindList() {
      const projectId = this.$store.getters['bimInfo/GET_PROJECTID']
      if (!projectId) return
      if (this.loading.isFetching) return
      this.loading.isFetching = true
      const params = {
        pageNo: 1,
        pageSize: 99999,
        businessId: projectId,
      }
      const [err, res] = await getBindList(params)
      this.loading.isFetching = false
      if (!err) {
        this.modelList = res.data || []
      }
    },
    handleConfirm() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          this.$message.warning("请填写完所有必填项信息")
          return
        }
        this.$emit('model:select', this.formInfo.modelId)
      })
    },
  },
}
</script>

<style>
</style>
